package cn.invest.merry.module.invest.service.lrbase;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.invest.merry.module.invest.controller.admin.lrbase.vo.*;
import cn.invest.merry.module.invest.dal.dataobject.lrbase.LrBaseDO;
import cn.invest.merry.framework.common.pojo.PageResult;
import cn.invest.merry.framework.common.pojo.PageParam;
import cn.invest.merry.framework.common.util.object.BeanUtils;

import cn.invest.merry.module.invest.dal.mysql.lrbase.LrBaseMapper;

import static cn.invest.merry.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.invest.merry.module.invest.enums.ErrorCodeConstants.*;

/**
 * 个股报告期利润 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LrBaseServiceImpl implements LrBaseService {

    @Resource
    private LrBaseMapper lrBaseMapper;

    @Override
    public Long createLrBase(LrBaseSaveReqVO createReqVO) {
        // 插入
        LrBaseDO lrBase = BeanUtils.toBean(createReqVO, LrBaseDO.class);
        lrBaseMapper.insert(lrBase);
        // 返回
        return lrBase.getId();
    }

    @Override
    public void updateLrBase(LrBaseSaveReqVO updateReqVO) {
        // 校验存在
        validateLrBaseExists(updateReqVO.getId());
        // 更新
        LrBaseDO updateObj = BeanUtils.toBean(updateReqVO, LrBaseDO.class);
        lrBaseMapper.updateById(updateObj);
    }

    @Override
    public void deleteLrBase(Long id) {
        // 校验存在
        validateLrBaseExists(id);
        // 删除
        lrBaseMapper.deleteById(id);
    }

    private void validateLrBaseExists(Long id) {
        if (lrBaseMapper.selectById(id) == null) {
            throw exception(LR_BASE_NOT_EXISTS);
        }
    }

    @Override
    public LrBaseDO getLrBase(Long id) {
        return lrBaseMapper.selectById(id);
    }

    @Override
    public PageResult<LrBaseDO> getLrBasePage(LrBasePageReqVO pageReqVO) {
        return lrBaseMapper.selectPage(pageReqVO);
    }

}